<?php
/**
 * Created by PhpStorm.
 * User: wanglei
 * Date: 2016/7/30
 * Time: 15:36
 * 公共类库
 */
class common {

    public function __construct() {

    }
    /**
     * 通讯录文件下载
     * @param $fileNmae文件名称 string
     * @param $data 数据 array(array('name'=>'wl','tel'=>13765489514),.............)
     *
     */
    public function downloadVcard($fileNmae,$data)
    {
        $file_content = "";
        $begin = "BEGIN:VCARD\r\nVERSION:3.0\r\n";
        $name = "N:;";
        $tel = "TEL:";
        $end = "END:VCARD\r\n";
        $data = $data?$data:array();
        foreach($data as $v){
            $file_content .= $begin;

//            $file_content .= $name.$v['nickname'].";;; \r\n";
            $file_content .= $name.'临时存储'.$v['userid'].";;; \r\n";

            $file_content .= $tel.$v['username']."\r\n";
            $file_content .= $end;
        }
        $fileNmae = $fileNmae.'_'.time();

        $path = pc_base::load_config('system','upload_path');
        $real_path = $path."vcard/";
        $handel=@fopen($real_path.$fileNmae.".vcf","w");
        $write=@fwrite($handel,$file_content,strlen($file_content));
        @fclose($handel);
        if($write){
            header("Content-type: text/directory");
            header("Content-Disposition: attachment; filename=".$fileNmae.".vcf"."");
            header('Content-Length:'.filesize($fileNmae.".vcf"));
            header("Pragma: public");
            echo $file_content;
        }else{
            echo '';
        }

    }
    /**
     *支付成功，回调
     * $out_trade_no 订单号
     * $trade_no 支付宝交易号
     */
    public function payCallback($out_trade_no,$trade_no)
    {
        $member_model = pc_base::load_model('member_model');
        $record_model = pc_base::load_model('shuafen_recharge_record_model');
        $record = $record_model->get_one(array('out_trade_no'=>$out_trade_no,'trade_status'=>'unpaid'));
        if(!$record) return false;
        $money = $record['money'];
        $userid = $record['member_id'];
        $result = array();

        //1.修改用户为vip用户，并给余额充值
        $result[] = $member_model->update("`vip`='Y',`amount`=`amount`+".$money,array('userid'=>$userid));
        //2.修改支付状态，并记录支付宝交易号
        $result[] = $record_model->update(array('trade_no'=>$trade_no,'trade_status'=>'paid','pay_time'=>time()),array('out_trade_no'=>$out_trade_no));

        if(!in_array(false,$result)){
            return true;
        }else{
            return false;
        }
    }
}